package xyz.xtt.pay.service.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.client.AssetClient;
import xyz.xtt.asset.client.request.CancelTransactionRequest;
import xyz.xtt.asset.client.request.ConfirmTransactionRequest;
import xyz.xtt.asset.client.request.MakeTransactionRequest;
import xyz.xtt.asset.client.response.AssetAccountResponse;
import xyz.xtt.asset.client.response.AssetAllInfoResponse;
import xyz.xtt.asset.client.response.AssetExtInfoResponse;
import xyz.xtt.asset.client.response.AssetInfoResponse;
import xyz.xtt.asset.client.response.AssetNoResponse;
import xyz.xtt.asset.client.response.AssetStaticResponse;
import xyz.xtt.asset.client.response.AssetTransactionResponse;
import xyz.xtt.asset.client.response.AssetUserLogResponse;
import xyz.xtt.asset.client.response.CancelOrderResponse;
import xyz.xtt.asset.client.response.ConfirmOrderResponse;
import xyz.xtt.asset.client.response.TransactionResponse;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.api.dto.GetBenefitPurchaseRecordRequest;
import xyz.xtt.equity.api.dto.GetOnSaleBenefitPackageRequest;
import xyz.xtt.equity.api.dto.SaveBenefitPurchaseUnPayDto;
import xyz.xtt.equity.api.dto.TblBenefitPackageDto;
import xyz.xtt.equity.api.dto.TblBenefitPurchaseRecordDto;
import xyz.xtt.equity.client.EquityClient;
import xyz.xtt.equity.client.request.GetUnPayBenefitPurchaseRecordRequest;
import xyz.xtt.equity.client.request.HandlePayCancelRequest;
import xyz.xtt.equity.client.request.HandlePaySuccessRequest;
import xyz.xtt.equity.client.request.TblUserBenefitDto;
import xyz.xtt.exchange.client.ExchangeClient;
import xyz.xtt.exchange.response.AssetAuditResponse;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.pay.service.IInnerService;
import xyz.xtt.user.client.UserClient;
import xyz.xtt.user.client.response.UserInfoResponse;

@Slf4j
@Service
public class IInnerServiceImpl implements IInnerService {
	@Autowired
	private AssetClient assetClient;
	@Autowired
	private UserClient userClient;
	@Autowired
	private EquityClient equityClient;
	@Autowired
	private ExchangeClient exchangeClient;

	@Override
	public AssetInfoResponse infoWithAssetId(Long assetId) {
		ApiResult<AssetInfoResponse> response = assetClient.infoWithAssetId(assetId);
		log.info("assetClient infoWithAssetId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetExtInfoResponse extInfoWithAssetId(Long assetId) {
		ApiResult<AssetExtInfoResponse> response = assetClient.extInfoWithAssetId(assetId);
		log.info("assetClient extInfoWithAssetId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAccountResponse accountInfoCheckAmount(Long assetId, Long appUid) {
		ApiResult<AssetAccountResponse> response = assetClient.accountInfoCheckAmount(assetId, appUid);
		log.info("assetClient accountInfoCheckAmount response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAccountResponse accountInfoUnCheckAmount(Long assetId, Long appUid) {
		ApiResult<AssetAccountResponse> response = assetClient.accountInfoUnCheckAmount(assetId, appUid);
		log.info("assetClient accountInfoUnCheckAmount response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TransactionResponse queryPaySyncOne(String buyUserId, Long orderId) {
		ApiResult<TransactionResponse> response = assetClient.queryPaySyncOne(buyUserId, orderId);
		log.info("assetClient queryPaySyncOne response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAccountResponse accountInfoWithId(Long accountId) {
		ApiResult<AssetAccountResponse> response = assetClient.accountInfoWithId(accountId);
		log.info("assetClient accountInfoWithId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean saveUpChainFlag(String appUid, Long businessRefId) {
		ApiResult<Boolean> response = assetClient.saveUpChainFlag(appUid, businessRefId);
		log.info("assetClient saveUpChainFlag response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public AssetStaticResponse staticInfoWithAssetId(Long assetId) {
		ApiResult<AssetStaticResponse> response = assetClient.staticInfoWithAssetId(assetId);
		log.info("assetClient staticInfoWithAssetId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetInfoResponse infoWithGoodsId(String goodsId) {
		ApiResult<AssetInfoResponse> response = assetClient.infoWithGoodsId(goodsId);
		log.info("assetClient infoWithGoodsId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAllInfoResponse allInfoAssetId(Long assetId) {
		ApiResult<AssetAllInfoResponse> response = assetClient.infoAssetId(assetId);
		log.info("assetClient allInfoAssetId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean hasGift365(Long appUid) {
		ApiResult<Boolean> response = assetClient.hasGift365(appUid);
		log.info("assetClient hasGift365 response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public TransactionResponse queryNotPayOrder(Long assetId, String appUid) {
		ApiResult<TransactionResponse> response = assetClient.queryNotPayOrder(assetId, appUid);
		log.info("assetClient queryNotPayOrder response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetTransactionResponse makeTransaction(MakeTransactionRequest makeOrder) {
		ApiResult<AssetTransactionResponse> response = assetClient.makeTransaction(makeOrder);
		log.info("assetClient makeTransaction response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public ConfirmOrderResponse confirmTransaction(ConfirmTransactionRequest confirmTransactionRequest) {
		ApiResult<ConfirmOrderResponse> response = assetClient.confirmTransaction(confirmTransactionRequest);
		log.info("assetClient confirmTransaction response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public CancelOrderResponse cancelTransaction(CancelTransactionRequest cancelTransactionRequest) {
		ApiResult<CancelOrderResponse> response = assetClient.cancelTransaction(cancelTransactionRequest);
		log.info("assetClient cancelTransaction response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TblUserBenefitDto getLatestUserBenefit(String uid, String benefitCode) {
		ApiResult<TblUserBenefitDto> response = equityClient.getLatestUserBenefit(uid, benefitCode);
		log.info("equityClient getLatestUserBenefit response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TransactionResponse transactionInfo(Long orderId) {
		ApiResult<TransactionResponse> response = assetClient.transactionInfo(orderId);
		log.info("equityClient transactionInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TblBenefitPackageDto getOnSaleBenefitPackage(GetOnSaleBenefitPackageRequest request) {
		ApiResult<TblBenefitPackageDto> response = equityClient.getOnSaleBenefitPackage(request);
		log.info("equityClient getOnSaleBenefitPackage response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TblBenefitPurchaseRecordDto getBenefitPurchaseRecord(Long uid, GetBenefitPurchaseRecordRequest request) {
		ApiResult<TblBenefitPurchaseRecordDto> response = equityClient.getBenefitPurchaseRecord(uid, request);
		log.info("equityClient getBenefitPurchaseRecord response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean handlePaySuccess(Long uid, HandlePaySuccessRequest request) {
		ApiResult<Boolean> response = equityClient.handlePaySuccess(uid, request);
		log.info("equityClient handlePaySuccess response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean handlePayCancel(Long uid, HandlePayCancelRequest request) {
		ApiResult<Boolean> response = equityClient.handlePayCancel(uid, request);
		log.info("equityClient handlePayCancel response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public TblBenefitPurchaseRecordDto getUnPayBenefitPurchaseRecord(Long uid, GetUnPayBenefitPurchaseRecordRequest request) {
		ApiResult<TblBenefitPurchaseRecordDto> response = equityClient.getUnPayBenefitPurchaseRecord(uid, request);
		log.info("equityClient getUnPayBenefitPurchaseRecord response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Long saveUnPayBenefitPurchase(Long uid, SaveBenefitPurchaseUnPayDto saveDto) {
		ApiResult<Long> response = equityClient.saveUnPayBenefitPurchase(uid, saveDto);
		log.info("equityClient saveUnPayBenefitPurchase response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, 0L);
	}

	@Override
	public UserInfoResponse userInfo(Long uid) {
		ApiResult<UserInfoResponse> response = userClient.info(uid);
		log.info("equityClient userInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<UserInfoResponse> batchInfo(Collection<Long> appUids) {
		ApiResult<List<UserInfoResponse>> response = userClient.batchInfo(appUids);
		log.info("equityClient batchInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public AssetAllInfoResponse allInfoGoodsId(String goodsId) {
		ApiResult<AssetAllInfoResponse> response = assetClient.infoGoodsId(goodsId);
		log.info("assetClient infoGoodsId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetUserLogResponse lastConfig(Long assetId) {
		ApiResult<AssetUserLogResponse> response = assetClient.lastConfig(assetId);
		log.info("assetClient lastConfig response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetNoResponse assetNoInfoWithNoId(Long noId) {
		ApiResult<AssetNoResponse> response = assetClient.assetNoInfoWithNoId(noId);
		log.info("assetClient assetNoInfoWithNoId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAllInfoResponse benefitPackageBaseInfo(Long benefitPackageId) {
		ApiResult<AssetAllInfoResponse> response = equityClient.benefitPackageBaseInfo(benefitPackageId);
		log.info("equityClient benefitPackageBaseInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAuditResponse assetAuditInfo(Long assetAuditId) {
		ApiResult<AssetAuditResponse> response = exchangeClient.assetAuditInfo(assetAuditId);
		log.info("exchangeClient assetAuditInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean handleOrderPaySuccess(TblPayOrderDto payOrderDto) {
		ApiResult<Boolean> response = userClient.handleOrderPaySuccess(payOrderDto);
		log.info("userClient handleOrderPaySuccess response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public List<TblBenefitPackageDto> getPackageByIds(List<Long> packageIds) {
		ApiResult<List<TblBenefitPackageDto>> response = equityClient.getPackageByIds(packageIds);
		log.info("equityClient getPackageByIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

}
